package com.st.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.st.enity.Dept;
import com.st.service.IDeptService;
import com.st.service.impl.DeptServiceImpl;

@WebServlet("/dept.do")
public class DeptServlet extends BaseServlet {
	private IDeptService deptService = new DeptServiceImpl();

	public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List depts = deptService.getAll();
		request.setAttribute("depts", depts);
		request.getRequestDispatcher("dl.jsp").forward(request, response);
	}
	public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String did =request.getParameter("did");
		int id = Integer.parseInt(did);
		int depts = deptService.remove(id);
		response.setContentType("text/html; charset=utf-8");
		if (depts > 0) {
			// 删除成功
			response.getWriter().write("<script>alert('删除成功'); window.location.href='dl.jsp';</script>");
		} else {
			// 删除失败
			response.getWriter().write("<script>alert('删除失败'); window.location.href='dl.jsp';</script>");
		}
	}
	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String key = request.getParameter("key");

		List depts = deptService.search(key);
		
		request.setAttribute("depts", depts);
		request.getRequestDispatcher("dl.jsp").forward(request, response);
		
	}
	public void change(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String did =request.getParameter("did");
		int id = Integer.parseInt(did);
		String Dname = request.getParameter("dname");
		String Dphone = request.getParameter("dphone");
		String Droom = request.getParameter("droom");
		Dept dept = new Dept();
		dept.setDname(Dname);
		dept.setDphone(Dphone);
		dept.setDroom(Droom);
		int depts = deptService.update(dept,id);
		System.out.println();
		response.setContentType("text/html; charset=utf-8");
		if (depts > 0) {
			response.getWriter().write("<script>alert('修改成功'); window.location.href='dl.jsp';</script>");
		} else {
			response.getWriter().write("<script>alert('修改失败'); window.location.href='dl.jsp';</script>");
		}

	}
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String did =request.getParameter("did");
		int id = Integer.parseInt(did);
		String Dname = request.getParameter("dname");
		String Dphone = request.getParameter("dphone");
		String Droom = request.getParameter("droom");
		Dept dept = new Dept();
		dept.setDname(Dname);
		dept.setDphone(Dphone);
		dept.setDroom(Droom);
		int depts = deptService.add(dept);
		System.out.println();
		response.setContentType("text/html; charset=utf-8");
		if (depts > 0) {
			response.getWriter().write("<script>alert('添加成功'); window.location.href='dl.jsp';</script>");
		} else {
			response.getWriter().write("<script>alert('添加失败'); window.location.href='dl.jsp';</script>");
		}

	}
}
